package com.example.hellodemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class ToastActivity extends AppCompatActivity {
    private Button btnToast1,btnToast2,btnToast3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
        btnToast1 = findViewById(R.id.btn_toast_1);
        btnToast2 = findViewById(R.id.btn_toast_2);
        btnToast3 = findViewById(R.id.btn_toast_3);
        addListers();

    }
    public void addListers(){
        MyOnClickListener listener = new  MyOnClickListener();
        btnToast1.setOnClickListener(listener);
        btnToast2.setOnClickListener(listener);
        btnToast3.setOnClickListener(listener);

    }

    class  MyOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch (v.getId()) {
                case R.id.btn_toast_1:
                    Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_toast_2:
                    Toast centerToast = Toast.makeText(getApplicationContext(), "居中Toast", Toast.LENGTH_SHORT);
                    centerToast.setGravity(Gravity.CENTER, 0, 0);
                    centerToast.show();
                    break;
                case R.id.btn_toast_3:
                    Toast customToast = new Toast(getApplicationContext());
                    customToast.setGravity(Gravity.CENTER, 0, 0);
                    LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
                    View view = inflater.inflate(R.layout.toast_custom, null);
                    ImageView img = view.findViewById(R.id.toast_img);
                    TextView textview = view.findViewById(R.id.toast_title);
                    img.setImageResource(R.drawable.icon_logo);
                    textview.setText("自定义Toast");
                    customToast.setView(view);
                    customToast.setDuration(Toast.LENGTH_LONG);
                    customToast.show();

                    break;
            }

        }
    }


}
